package beans;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;
import java.beans.VetoableChangeSupport;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Forum implements Serializable {
    
    private String titel;
    private List<String> themen = new ArrayList<>();
    private PropertyChangeSupport zuhoerer = 
            new PropertyChangeSupport(this);
    private VetoableChangeSupport aktiveMitentscheider = 
            new VetoableChangeSupport(this);
    private int vetoChangeCounter = 0;
    private int vetoThrowCounter = 0;
    private int propertyChangeCounter = 0;

    public Forum() {
    }

    /**
     * @return the titel
     */
    public String getTitel() {
        return titel;
    }

    /**
     * @param titel the titel to set
     */
    public void setTitel(String titel) {
        this.titel = titel;
    }
    
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        zuhoerer.addPropertyChangeListener(listener);
        propertyChangeCounter++;
    }
    
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        zuhoerer.removePropertyChangeListener(listener);
        propertyChangeCounter--;
    }
    
    public void addVetoableChangeListener(VetoableChangeListener listener) {
        aktiveMitentscheider.addVetoableChangeListener(listener);
        vetoChangeCounter++;
    }
    
    public void removeVetoableChangeListener(VetoableChangeListener listener) {
        aktiveMitentscheider.removeVetoableChangeListener(listener);
        vetoChangeCounter--;
    }

    /**
     * @return the thema
     */
    public ArrayList<String> getThemen() {
        return (ArrayList) themen;
    }

    
    
    public void addThema(String neues_thema) {
        //List<String> themen_alt = (ArrayList) copyStringList(this.themen);
        List<String> themen_neu = (ArrayList) copyStringList(this.themen);
        themen_neu.add(neues_thema);
        
        try {
            //Erst nach Vetos fragen
            aktiveMitentscheider.fireVetoableChange("themen", this.themen, themen_neu);
            
            //Wenn kein Veto, dann Änderung als Bound Property behandeln
            zuhoerer.firePropertyChange("themen", this.themen, themen_neu);
            this.themen = themen_neu;
        
        //wenn Veto, dann Änderung abbrechen
        } catch(PropertyVetoException e) {
            vetoThrowCounter++;
            System.out.println(e.getMessage());
        }
    }
    
    /**
     * Tiefes Kopieren eines HashSets mit Strings
     * @param original Zu kopierendes HashSet
     * @return Kopie der orginalen HashSets
     */
    private List<String> copyStringList(List<String> original) {
        ArrayList<String> ergebnis = new ArrayList<>();
        for(String elem : original) {
            ergebnis.add(new StringBuffer(elem).toString());
        }
        return ergebnis;
    }
    
    @Override
    public String toString() {
        StringBuilder s = new StringBuilder();
        s.append("Forum ").append(this.titel).append("\nVerabschieded:\n");
        for(String elem : this.themen) {
            s.append(elem).append("\n");
        }
        return s.toString();
    }

    /**
     * @return the vetoChangeCounter
     */
    public int getVetoChangeCounter() {
        return vetoChangeCounter;
    }

    /**
     * @return the propertyChangeCounter
     */
    public int getPropertyChangeCounter() {
        return propertyChangeCounter;
    }

    /**
     * @return the vetoThrowCounter
     */
    public int getVetoThrowCounter() {
        return vetoThrowCounter;
    }
}
